package edu.sysu.cshare.model;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import edu.sysu.cshare.util.HibernateSessionFactory;
import edu.sysu.cshare.util.Mis;

@Entity
@Table(name = "T_USER")
public class User implements Serializable {

	public static User getById(String id) {
		return (User) HibernateSessionFactory.getSession().load(User.class, id);
	}

	private Clazz clazz;

	private List<Comment> comments = new ArrayList<Comment>();

	private Set<Course> courses = new HashSet<Course>();

	private String descrip;

	private Set<Group> groups = new HashSet<Group>();
	private String id;

	private String lastIP;

	private List<Message> messages = new ArrayList<Message>();

	private String name;

	private String nickname;

	private String password;

	/**
	 * 1=普通学生 3=学委 4=老师或者助教 7=系统管理员 所有人都能登录并发布信息 第0位->能提交作业,且能使用共享
	 * 第1位->能管理班级内的用户和课程 第2位->能添加删除下载作业
	 */
	private int role;

	private List<SubmitInfo> submitInfos = new ArrayList<SubmitInfo>();

	public User() {

	}

	/**
	 * 简单的构造函数 默认学生身份 默认密码为学号后四位,默认描述为空
	 * 
	 * @param id
	 *            学号
	 * @param name
	 *            姓名
	 */
	public User(String id, String name) {
		super();
		this.id = id;
		this.name = name;
		this.role = 1;
		this.password = Mis.getMD5(id.length() == 8 ? id.substring(4) : id);
	}

	/**
	 * 构造函数
	 * 
	 * @param id
	 *            一般是学号
	 * @param name
	 *            用户姓名
	 * @param password
	 *            明文密码,在构造函数中进行MD5加密
	 * @param descrip
	 *            描述
	 * @param clazz
	 *            所属班级 老师不属于一个班级
	 */
	public User(String id, String name, String nickname, String password,
			int role, String descrip, Clazz clazz) {
		super();
		this.descrip = descrip;
		this.nickname = nickname;
		this.id = id;
		this.role = role;
		this.name = name;
		this.clazz = clazz;
		this.password = Mis.getMD5(password);
	}

	public boolean canManageClass() {
		return ((role & 2) != 0);
	}

	public boolean canManageHomework() {
		return ((role & 4) != 0);
	}

	public boolean canShare() {
		return canSubmit();
	}

	public boolean canSubmit() {
		return ((role & 1) != 0);
	}

	@ManyToOne
	public Clazz getClazz() {
		return clazz;
	}

	@OneToMany(mappedBy = "commenter")
	public List<Comment> getComments() {
		return comments;
	}

	@ManyToMany(targetEntity = Course.class)
	
	@JoinTable(name = "TR_USER_COURSE", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "course_id")

	)
	public Set<Course> getCourses() {
		return courses;
	}

	public String getDescrip() {
		return descrip;
	}

	/*	public void joinGroup(Group group) throws Exception {
			group.addMember(this);
		}

		public void quitGroup(Group group) throws Exception {
			group.removeMember(this);
		}
	*/
	public Group getGroupByCourse(Course course) {
		for (Group g : this.groups) {
			if (g.getCourse().getId() == course.getId()) {
				return g;
			}
		}
		return null;
	}

	@ManyToMany(mappedBy = "members", targetEntity = Group.class)
	public Set<Group> getGroups() {
		return groups;
	}

	@Id
	@Column(length = 20)
	public String getId() {
		return id;
	}

	@Column(length = 15)
	public String getLastIP() {
		/*
		try {
			lastIP = InetAddress.getLocalHost().getHostAddress().toString();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}*/
		return lastIP;

	}

	@OneToMany(mappedBy = "publisher")
	public List<Message> getMessages() {
		return messages;
	}

	@Column(length = 50)
	public String getName() {
		return name;
	}

	@Column(length = 30)
	public String getNickname() {
		return nickname;
	}

	@Column(length = 50)
	public String getPassword() {
		return password;
	}

	public int getRole() {
		return role;
	}

	/**
	 * 指定Homework, 返回相应的SubmitInfo
	 * 
	 * @param homework
	 */
	public SubmitInfo getSubmitInfoByHomework(Homework homework) {
		for (SubmitInfo s : submitInfos) {
			if (s.getHomework().getId() == homework.getId())
				return s;
		}
		return null;
	}

	@OneToMany(mappedBy = "user")
	public List<SubmitInfo> getSubmitInfos() {
		return submitInfos;
	}

	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}

	public void setDescrip(String descrip) {
		this.descrip = descrip;
	}

	public boolean isGroupped(Course course){
		for(Group g :groups){
			if(g.getCourse().getId()==course.getId())
				return true;
		}
		return false;
	}
	public void setGroups(Set<Group> groups) {
		this.groups = groups;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setLastIP(String lastIP) {
		this.lastIP = lastIP;
	}

	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setRole(int role) {
		this.role = role;
	}

	public void setSubmitInfos(List<SubmitInfo> submitInfos) {
		this.submitInfos = submitInfos;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", role=" + role + "]";
	}
}
